package com.cfswt.tasksystem.controller;

import com.cfswt.tasksystem.mapper.DemandSupporterMapper;
import com.cfswt.tasksystem.mapper.DemandsMapper;
import com.cfswt.tasksystem.pojo.entity.Demand;
import com.cfswt.tasksystem.pojo.entity.DemandSupporter;
import com.cfswt.tasksystem.pojo.entity.DemandSupporterWrapper;
import com.cfswt.tasksystem.pojo.vo.DemandDetailVO;
import com.cfswt.tasksystem.pojo.vo.DemandSupporterDetailVO;
import com.cfswt.tasksystem.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/demand")
@Api(tags = "40.需求管理模块")
public class DemandContorller {

    @Autowired
    private DemandsMapper demandsMapper;
    @Autowired
    private DemandSupporterMapper demandSupporterMapper;
    @ApiOperation(value = "新增需求")
    @PostMapping("/add")
    public JsonResult addDemand(@RequestBody DemandSupporterWrapper wrapper){
        Demand demand = wrapper.getDemand();
        List<DemandSupporter> demandSupporters = wrapper.getDemandSupporters();
        demand.setCreateTime(new Date());
        demand.setUpdateTime(new Date());
        demandsMapper.addDemand(demand);
        Long demandId = demand.getId();
        for (DemandSupporter demandSupporter : demandSupporters) {
            demandSupporter.setDemandId(demandId);
            demandSupporterMapper.addSupporter(demandSupporter);
        }
        return JsonResult.ok(wrapper);
    }
    @ApiOperation(value = "根据编号查找需求详细信息")
    @GetMapping("/{sn}")
    public JsonResult selectDemandDetail(@PathVariable String sn){
        DemandDetailVO demandDetailVO = demandsMapper.selectDemandDetailVOBySn(sn);
        List<DemandSupporterDetailVO> demandSupporterDetailVOS = demandSupporterMapper.selectDemandSupporterDetailVOByDemandId(demandDetailVO.getId());
        demandDetailVO.setSupporter(demandSupporterDetailVOS);
        System.out.println(demandSupporterDetailVOS);
        System.out.println(demandDetailVO);
        return JsonResult.ok(demandDetailVO);
    }
    @PostMapping("/startProcess")
    public JsonResult startProjectProcess(){


        return JsonResult.ok();
    }
}
